package com.jzsm.modules.sender.controller;

import com.jzsm.common.controller.BaseController;
import com.jzsm.common.core.domain.AjaxResult;
import com.jzsm.common.core.page.TableDataInfo;
import com.jzsm.common.utils.AssertUtils;
import com.jzsm.modules.sender.holder.PushHolder;
import com.jzsm.modules.sender.pojo.PushTemplate;
import com.jzsm.modules.sender.service.IPushTemplateService;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("template")
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class PushTemplateController extends BaseController {
    final IPushTemplateService pushTemplateService;
    final PushHolder pushHolder;

    /**
     * 查询列表
     *
     * @param pushTemplate 查询参数
     */
    @GetMapping("list")
    public AjaxResult<TableDataInfo<PushTemplate>> getList(PushTemplate pushTemplate) {
        startPage();
        return AjaxResult.success(getDataTable(pushTemplateService.selectList(pushTemplate)));
    }

    /**
     * 根据 group id 进行查询列表
     *
     * @param groupId      集团ID
     * @param pushTemplate 模板ID
     * @return
     */
    @GetMapping("list/{groupId}")
    public AjaxResult<TableDataInfo<PushTemplate>> getListByGroupId(
            @PathVariable("groupId") String groupId,
            PushTemplate pushTemplate
    ) {
        AssertUtils.isNotBlank("groupId 不允许为空！", groupId);
        pushTemplate.setPrivacyGroupId(groupId);
        startPage();
        return AjaxResult.success(getDataTable(pushTemplateService.selectList(pushTemplate)));
    }

    /**
     * 根据模板ID 推送
     *
     * @param templateId 模板ID
     * @param data       推送
     * @param businessId 业务ID可用于查询推送记录 推送方持有的唯一ID
     * @return
     */
    @PostMapping("push/{templateId}/business/{businessId}")
    public AjaxResult<String> templateById(
            @PathVariable("templateId") String templateId,
            @PathVariable("businessId") String businessId,
            @RequestBody Map<String, Object> data
    ) {
        return AjaxResult.success(pushHolder.pushByTemplateId(templateId, businessId, data));
    }
}
